<template>
  <el-select v-model="selectId" style="width: 260px;" filterable :clearable="false">
    <el-option v-for="item in arr" :key="item.id" :label="item.companyName" :value="item.id">
    </el-option>
  </el-select>
</template>

<script>

import Vue from 'vue'


export default {
  data() {
    return {
      selectId: '',
      dataMap: {},
      arr: [],

      userInfo: __LONG_USER__
    }
  },

  watch: {

    'selectId'(val, old) {
      let row = this.dataMap[val]
      if (!row) {
        return
      }

      Vue.prototype.$currentAgentId = val

      if (this.userInfo.currentAgentId != val) {
        this.$message.success(this.$tt("tip_success_switched_to_refresh_page"))
        this.$ajax({
          url: '/ums-api/user/CookieRpc/changeCurrentAgent.json',
          loading: true,
          data: { agentId: val },
          success: function (res) {

            window.setTimeout(() => {
              window.location.reload()
            }, 100)
          }
        })

      }

    }
  },

  created() {


    let currentAgentId = this.userInfo.currentAgentId

    Vue.prototype.$currentAgentId = currentAgentId

    this.$ajax({
      url: '/ums-api/user/UserCompanyRpc/selectAllAgent.json',
      method: 'post',
      data: { status: 'ENABLE' },
      success: (res) => {

        if (res.data && res.data.rows) {

          res.data.rows.forEach(e => {
            this.dataMap[e.id] = e
          })

          this.arr = res.data.rows

          if (currentAgentId && this.dataMap[currentAgentId]) {
            this.selectId = currentAgentId
          }

          if (this.selectId == '' && this.arr.length > 0) {
            this.selectId = this.arr[0].id
          }
        }
      },
    })
  },

  methods: {}
}
</script>
